
import gtk
import pygtk
import gobject

import resources

class UiProgress:
    def __init__(self, prog):
        self.__prog = prog

        self.__total = 0
        self.__worked = 0

    def set_work_remaining(self, total):
        self.__total = total

    def worked(self, message, count):
        self.__worked += count
        fraction = float(self.__worked) / float(self.__total)
        
        def set():
            if message:
                self.__prog.set_text(message)
            self.__prog.set_fraction(fraction)
        gobject.idle_add(set)

    def new_child(self, units):
        return self

class TrayIcon:
    """Encapsulates the tray icon's behaviour."""

    def __init__(self):
        self.__icon = gtk.StatusIcon()
        self.__update_size()

        self.__icon.set_visible(True)
        self.__icon.connect('popup-menu', self.__menu_cb)
        self.__icon.connect('activate', self.__activate)
        self.__icon.connect('size-changed', self.__update_size)

    def __activate(self, icon, widget=None):
        print "Activate"
        if self.__cb_show:
            self.__cb_show()

    def set_accounts_callback(self, cb):
        """Set the callback to run when the "Accounts" item is clicked."""
        self.cb_accounts = cb

    def set_add_account_callback(self, cb):
        """Set the callback to run when the "Add Accounts" item is clicked."""
        self.__cb_add_account = cb

    def set_refresh_callback(self, cb):
        """Set the callback to run when "Refresh" is clicked."""
        self.__cb_refresh = cb

    def set_show_callback(self, cb):
        """Set the callback to run when "Show" is clicked."""
        self.__cb_show = cb

    def set_configure_callback(self, cb):
        """Set the callback to run when "Configure" is clicked."""
        self.__cb_configure = cb

    def set_exit_callback(self, cb):
        """Set the callback to run when the user quits."""
        self.__cb_exit = cb

    def __menu_cb(self, icon, button, time):
        """Show the menu"""
        menu = gtk.Menu()

        # Add the default action
        menuItem = gtk.MenuItem("Show Balances...")
        menuItem.connect('activate', self.__activate)

        menu.append(menuItem)

        # Force a refresh
        menuItem = gtk.ImageMenuItem(gtk.STOCK_REFRESH)
        menuItem.get_children()[0].set_label("Reload Accounts")
        menuItem.connect('activate', self.__cb_refresh)

        menu.append(menuItem)


        # Add a separator
        menuItem = gtk.MenuItem()

        menu.append(menuItem)


        # Add acct
        menuItem = gtk.ImageMenuItem(gtk.STOCK_ADD)
        menuItem.get_children()[0].set_label("Add Account...")
        menuItem.connect('activate', self.__cb_add_account)

        menu.append(menuItem)


        # Configure
        menuItem = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
        menuItem.get_children()[0].set_label("Configure...")
        menuItem.connect('activate', self.__cb_configure)

        menu.append(menuItem)


        # About
        menuItem = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
        menuItem.get_children()[0].set_label("About Balancer")
        menuItem.connect('activate', self.__cb_about)
        
        menu.append(menuItem)


        # About
        menuItem = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        menuItem.connect('activate', self.__cb_exit)
        
        menu.append(menuItem)


        menu.show_all()
        menu.popup(None, None, None, 3, time)


    def __cb_about(self, ignore):
        """Display an "About" dialog."""
        about = gtk.AboutDialog()

        about.set_name("Balancer")
        about.set_version("0.1.0 \"Modern Love\"")
        about.set_logo(None)

        about.set_artists(["VisualPharm http://www.visualpharm.com"])
        about.set_authors(["Erigami Scholey-Fuller http://piepalace.ca"])
        
        def die(dialog, response_id):
            """The user is done. Store the password and close the window."""
            about.destroy()

        about.connect("response", die)


        about.show_all()


    def set_error(self, error_string):
        """Inform the user that we have an error state."""


    def __update_size(self, icon=None, size=None):
        """Update the icon on a size change."""
        if not size:
            size = self.__icon.get_size()
        print size
        self.__icon.set_from_pixbuf(resources.sized_icon(resources.BASE_ICON, size))
